package com.smstransmit.control


import java.io.File
import java.util.Date
import java.util.Properties

import javax.activation.DataHandler
import javax.activation.FileDataSource
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.internet.AddressException
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart

/**
 * Created by shijie.guo on 2017/9/11.
 */
class EmailSender {
    private val properties: Properties
    private var session: Session? = null
    private var message: Message? = null
    private var multipart: MimeMultipart? = null

    init {
        this.properties = Properties()
    }

    fun setProperties(host: String, post: String) {
        //地址
        this.properties.put("mail.smtp.host", host)
        //端口号
        this.properties.put("mail.smtp.post", post)
        //是否验证
        this.properties.put("mail.smtp.auth", true)
        this.session = Session.getInstance(properties)
        this.message = MimeMessage(session)
        this.multipart = MimeMultipart("mixed")
    }

    /**
     * 设置收件人

     * @param receiver
     * *
     * @throws MessagingException
     */
    @Throws(MessagingException::class)
    fun setReceiver(receiver: Array<String>) {
        val address = arrayOfNulls<InternetAddress>(receiver.size)
        for (i in receiver.indices) {
            address[i] = InternetAddress(receiver[i])
        }
        this.message!!.setRecipients(Message.RecipientType.TO, address)
    }

    /**
     * 设置邮件

     * @param from    来源
     * *
     * @param title   标题
     * *
     * @param content 内容
     * *
     * @throws AddressException
     * *
     * @throws MessagingException
     */
    @Throws(AddressException::class, MessagingException::class)
    fun setMessage(from: String, title: String, content: String) {
        this.message!!.setFrom(InternetAddress(from))
        this.message!!.subject = title
        //纯文本的话用setText()就行，不过有附件就显示不出来内容了
        val textBody = MimeBodyPart()
        textBody.setContent(content, "text/html;charset=gbk")
        this.multipart!!.addBodyPart(textBody)
    }

    /**
     * 添加附件

     * @param filePath 文件路径
     * *
     * @throws MessagingException
     */
    @Throws(MessagingException::class)
    fun addAttachment(filePath: String) {
        val fileDataSource = FileDataSource(File(filePath))
        val dataHandler = DataHandler(fileDataSource)
        val mimeBodyPart = MimeBodyPart()
        mimeBodyPart.dataHandler = dataHandler
        mimeBodyPart.fileName = fileDataSource.name
        this.multipart!!.addBodyPart(mimeBodyPart)
    }

    /**
     * 发送邮件

     * @param host    地址
     * *
     * @param account 账户名
     * *
     * @param pwd     密码
     * *
     * @throws MessagingException
     */
    @Throws(MessagingException::class)
    fun sendEmail(host: String, account: String, pwd: String) {
        //发送时间
        this.message!!.sentDate = Date()
        //发送的内容，文本和附件
        this.message!!.setContent(this.multipart)
        this.message!!.saveChanges()
        //创建邮件发送对象，并指定其使用SMTP协议发送邮件
        val transport = session!!.getTransport("smtp")
        //登录邮箱
        transport.connect(host, account, pwd)
        //发送邮件
        transport.sendMessage(message, message!!.allRecipients)
        //关闭连接
        transport.close()
    }


}
